﻿function ClientManager() {

}

ClientManager.prototype = {
    getClientID: function(clientInfo) {
        return clientInfo.IP.split('.').join('_') + '_' + clientInfo.Port;
    },
    notifyClientStatusChanged: function(data, clientContainer) {
        console.log(new Date().now() + ' :' + data);

        var clientInfo = JSON.parse(data.substr(4));
        var clientID = this.getClientID(clientInfo);
        var clientEle = clientContainer.find('#client_' + clientID);

        if (clientEle.length == 0) {
            var newClientEle = this.createClientEle(clientID, clientInfo);
            var otherClientContainer = clientContainer.find('.otherClientContainer');
            otherClientContainer[0].appendChild(newClientEle);
        } else {
            this.updateClient(clientEle, clientInfo);
        }
    },
    updateClient: function(clientEle, clientInfo) {
        //最近访问时间
        clientEle.find('#lastConnectTime').text(clientInfo.LastConnectTime);
        //持续连接时间
        clientEle.find('#duration').text(clientInfo.Duration);
        //在线状态
        var iconStatus = clientEle.find('#iconStatus');
        var circleStatus = clientEle.find('#circleStatus');
        if (clientInfo.IsOnline == true) {
            iconStatus.removeClass('matrixIcon-offline').addClass('matrixIcon-online');
            circleStatus.removeClass('matrixCircle-offline').addClass('matrixCircle-online');
            circleStatus.attr('title', '在线');
        } else {
            iconStatus.removeClass('matrixIcon-online').addClass('matrixIcon-offline');
            circleStatus.removeClass('matrixCircle-online').addClass('matrixCircle-offline');
            circleStatus.attr('title', '离线');
        }
        //连接矩阵
        var linkMatrices = clientEle.find('#linkMatrices');
        if (linkMatrices.length > 0) {
            var linkMatricesEle = $(linkMatrices[0]);
            linkMatricesEle.empty();
            linkMatricesEle.text('');
            for (matrixId in clientInfo.ConnectedToMatrices) {
                var matrixEle = document.createElement('a');
                matrixEle.href = '/Matrix/MatrixDetail?matrixId=' + matrixId;
                matrixEle.text = matrixId + ':' + clientInfo.ConnectedToMatrices[matrixId];
                linkMatricesEle.append(matrixEle).append(',');
            }
            //移除最后一个逗号
            var last = linkMatricesEle[0].lastChild;
            if (last) {
                last.textContent = '';
            }
        }
    },
    createClientEle: function(clientID, clientInfo) {
        var imgEle = document.createElement('img');
        imgEle.id = 'iconStatus';
        imgEle.src = clientInfo.IsWebClient ? "/images/webclient.png" : "/images/client.png";
        imgEle.className = clientInfo.IsOnline ? 'matrixIcon-online' : 'matrixIcon-offline';

        var linkEle = document.createElement('a');
        linkEle.href = '/Client/ClientDetail?address=' + clientID;
        linkEle.appendChild(imgEle);

        var iconContainer = document.createElement('div');
        iconContainer.className = 'col-md-3';
        iconContainer.appendChild(linkEle);

        var dlEle = document.createElement('dl');
        dlEle.className = 'dl-horizontal matrixListItem-title';

        var childs = new Array(9);
        for (var attr in clientInfo) {
            var attributeItem = this.getAttributeItem(attr, clientInfo);
            if (!attributeItem.isEmpty) {
                var dt = document.createElement('dt');
                dt.innerText = attributeItem.title;

                var dd = document.createElement('dd');
                if (attributeItem.id) {
                    dd.id = attributeItem.id;
                }
                if (attributeItem.isHtml) {
                    dd.innerHTML = attributeItem.value;
                } else {
                    dd.innerText = attributeItem.value;
                }

                childs[attributeItem.index] = { dt: dt, dd: dd };
            }
        }

        for (index in childs) {
            dlEle.appendChild(childs[index].dt);
            dlEle.appendChild(childs[index].dd);
        }

        var clientInfoContainer = document.createElement('div');
        clientInfoContainer.className = 'col-md-9';
        clientInfoContainer.appendChild(dlEle);

        var clientEle = document.createElement('div');
        clientEle.id = 'client_' + clientID;
        clientEle.className = 'row clientListItem';
        clientEle.appendChild(iconContainer);
        clientEle.appendChild(clientInfoContainer);

        return clientEle;
    },
    getAttributeItem: function(attrName, clientInfo) {
        var title, index;
        switch (attrName) {
            case 'HostName':
                return {
                    index: 0,
                    isHtml: true,
                    title: '主机名',
                    value: '<a href="/Client/ClientDetail?address=' + this.getClientID(clientInfo) + '">' + clientInfo[attrName] + '</a>'
                };
            case 'IsOnline':
                var cls = clientInfo[attrName] === true ? 'matrixCircle-online' : 'matrixIcon-offline';
                var text = clientInfo[attrName] === true ? '在线' : '离线';
                return {
                    index: 8,
                    isHtml: true,
                    title: '在线状态',
                    value: '<div id="circleStatus" class="' + cls + '" title="' + text + '"></div>'
                };
            case 'ConnectedToMatrices':
                var linkMatrices = '';
                for (matrix in clientInfo.ConnectedToMatrices) {
                    linkMatrices += '<a href="/Matrix/MatrixDetail?matrixId=' + matrixId.Key + '">' + matrix.Key + ':' + matrix.Value + '</a>';
                }
                return {
                    index: 7,
                    isHtml: true,
                    title: '连接矩阵',
                    value: linkMatrices
                };
            case 'IP':
                index = 1;
                title = 'IP地址';
                break;
            case 'Port':
                index = 2;
                title = '端口';
                break;
            case 'UserID':
                index = 3;
                title = '用户ID';
                break;
            case 'FirstConnectTime':
                index = 4;
                title = '首次访问时间';
                break;
            case 'LastConnectTime':
                return { index: 5, title: '最近访问时间', value: clientInfo[attrName], id: 'lastConnectTime' };
            case 'Duration':
                return { index: 6, title: '持续连接时间', value: clientInfo[attrName], id: 'duration' };
            default:
                return { isEmpty: true };
        }

        return { index: index, title: title, value: clientInfo[attrName] };
    }
}